package com.admin.bank.service.impl;

import com.admin.bank.mapper.ArBankcardMapper;
import com.admin.bank.service.ArBankcardService;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.client.model.pojo.bank.ArBankcard;
import com.client.model.pojo.bank.ArCardtype;
import com.client.model.pojo.user.ArUser;
import com.client.model.vo.common.Result;
import com.client.model.vo.common.ResultCodeEnum;
import com.client.model.vo.deposit.BankVo;
import com.github.yulichang.wrapper.MPJLambdaWrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
* @author liuwenju
* @description 针对表【ar_bankcard】的数据库操作Service实现
* @createDate 2024-04-01 20:55:27
*/
@Service
public class ArBankcardServiceImpl extends ServiceImpl<ArBankcardMapper, ArBankcard>
    implements ArBankcardService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private ArBankcardMapper arBankcardMapper;
    @Override
    public Result findBank(String token) {
        ArUser arUser = JSON.parseObject(redisTemplate.opsForValue().get("user:login" + token), ArUser.class);
        List<BankVo> bankVos = arBankcardMapper.selectJoinList(BankVo.class,
                new MPJLambdaWrapper<ArBankcard>()
                        .selectAll(ArBankcard.class)
                        .selectAll(ArCardtype.class)
                        .innerJoin(ArCardtype.class, ArCardtype::getTid, ArBankcard::getTid)
                        .eq("uid",arUser.getUid()));
        if (bankVos == null){
            return Result.build(null,500,"尚未绑定银行卡");
        }
        return Result.build(bankVos, ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result savePrice(String bankCard, BigDecimal price) {
        ArBankcard arBankcard = arBankcardMapper.selectOne(new QueryWrapper<ArBankcard>()
                .eq("bank_card", bankCard));
        if (arBankcard == null){
            return Result.build(null,500,"银行卡不存在");
        }
        arBankcard.setBalance(arBankcard.getBalance().add(price));
        arBankcardMapper.updateById(arBankcard);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
}




